import axios from 'axios'
import Qs from 'qs'
import { Toast } from 'vant';
import router from '../router'
var root = process.env.NODE_ENV
axios.defaults.timeout = 10000

axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'

import store from '../store'


axios.interceptors.request.use( (config)=>{
    const token = store.state.token;
    // const token = 'SFCpuCk3kuUhvVjzeRI1gLyMY8YYV2rodlzswLgW6jBwubdsnn4q8ZvhHAG=XsW9k67yoThz9rCr9wXSLwlZwEX7\n'
    token && (config.headers.token = token);
    let host = ''
    if(root === 'development'){
        host = ''
    }else{
        // host = 'https://api.aat.ink'
        // host = 'http://106.75.117.187:8803/'
    }
    config.url = host + config.url
    return config;
},(error)=>{
    return Promise.reject(error)
})

axios.interceptors.response.use( (response)=>{
        if(response.status == 200){
            return Promise.resolve(response)
        }else{
            return Promise.reject(response);
        }
    },
    error => {
        if(error.response.status){
            switch (error.response.status) {
                case 401:
                    router.replace({
                        path: '/login',
                        query: {
                            redirect: router.currentRoute.fullPath
                        }
                    });
                    break;
                default:
                    Toast.fail('请联系后台')
            }
        }
    })

export default {
    get:function (url,params) {
        return new Promise((resolve,reject)=>{
            // axios.get(`http://106.75.117.187:8803/api/${url}`,{params:params})
            axios.get(`https://api.aat.ink/api/${url}`,{params:params})
                .then(res =>{
                    if(res.data.status === 0){
                        resolve(res.data)
                    }else if(res.data.status === 2){
                        Toast.fail(res.data.message)
                        router.replace({
                            path: '/login',
                            query: {
                                redirect: router.currentRoute.fullPath
                            }
                        });
                        localStorage.removeItem('user')
                        localStorage.removeItem('token')
                    }else{
                        Toast.fail(res.data.message)
                        return false
                    }
                })
                .catch(err =>{
                    reject(err.data)
                })
        })
    },
    post:function (url,params,self) {
        let lan = store.state.lan;
        console.log(Qs.stringify(params))
        return new Promise((resolve,reject)=>{
            // axios.post(`http://106.75.117.187:8803/api/${url}`,Qs.stringify(params),{
            axios.post(`https://api.aat.ink/api/${url}`,Qs.stringify(params),{
                headers: {
                    'Content-Type':'application/x-www-form-urlencoded'
                },
            }).then(res =>{
                    if(res.data.status === 0){
                        resolve(res.data)
                    }else if(res.data.status === 2){
                        Toast.fail(res.data.message)
                        router.replace({
                            path: '/login',
                            query: {
                                redirect: router.currentRoute.fullPath
                            }
                        });
                        localStorage.removeItem('user')
                        localStorage.removeItem('token')
                    }else{
                        Toast.fail(res.data.message)
                        self.loadingShow = false
                        return false
                    }
                })
                .catch(err =>{
                    reject(err.data)
                })
        })
    },

    gettest:function (url,params) {
        return new Promise((resolve,reject)=>{
            axios.get(`https://api.aat.ink/${url}`,{params:params})
                .then(res =>{
                    if(res.data.status === 0){
                        resolve(res.data)
                    }else if(res.data.status === 2){
                        Toast.fail(res.data.message)
                        router.replace({
                            path: '/login',
                            query: {
                                redirect: router.currentRoute.fullPath
                            }
                        });
                        localStorage.removeItem('user')
                        localStorage.removeItem('token')
                    }else{
                        Toast.fail(res.data.message)
                        return false
                    }
                })
                .catch(err =>{
                    reject(err.data)
                })
        })
    }
}
